<?php

/**
 * jobboard actions.
 *
 * @package    emc
 * @subpackage jobboard
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class jobboardActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }


  public function executeList(sfWebRequest $request)
  {
    $this->jobboards = Doctrine_Core::getTable('Jobboard')->findByIsActive(true);
    $this->notify =  $request->getParameter('notify');
  	return sfView::SUCCESS;
  }


  public function executeAccountList(sfWebRequest $request)
  {
    $jobboard_id = $request->getParameter('jobboard_id');
    $this->jobboard = Doctrine_Core::getTable('Jobboard')->find($jobboard_id);
    if ($this->jobboard) {
      $this->accounts = Doctrine_Core::getTable('Account')->findByJobboardIdAndIsActive($jobboard_id, true);
    }
    return sfView::SUCCESS;
  }


  public function executeAccountDelete(sfWebRequest $request)
  {
    $account_id = $request->getParameter('account_id');
    $account = Doctrine_Core::getTable('Account')->find($account_id);
    if ($account) {
      $account->is_active = false;
      $account->save();
    }
    $this->getRequest()->setParameter('jobboard_id', $account->Jobboard->id);
    $this->forward('jobboard', 'accountList');
  }


  public function executeAccountEdit(sfWebRequest $request)
  {
    $data = $request->getParameter('account', null);
    if ($data) {
        $this->jobboard_id = $request->getParameter('jobboard_id');
        $account = Doctrine_Core::getTable('Account')->findOneByJobboardIdAndUsername($this->jobboard_id, $data['username']);
        $is_reactivate = false;
        if (!$data['account_id'] && $account)
            if ($account->is_active == false) {
              $is_reactivate = true;
              $account->is_active = true;
              $account->save();
            } else {
              $this->notify = "account_exists";
              $this->form = new AccountForm();
              $this->form->bind($data);
              return sfView::SUCCESS;
            }
        if ($data['account_id'])
          $account = Doctrine_Core::getTable('Account')->find($data['account_id']);
        elseif (!$is_reactivate)
          $account = new Account();
          
        try {
          $account->username = $data['username'];
          $account->password = $data['password'];
          $account->jobboard_id = $this->jobboard_id;
          if (!$is_reactivate)
            $account->is_active = isset($data['is_active']) ? true : false;
          $account->save();
          $this->notify = $data['account_id'] ? "update" : "create"."_account_success";
          $this->form = new AccountForm($account);
        } catch (Exception $e) {
          $this->notify = "create_account_failed";
        }
      } else {
        $this->account_id = $request->getParameter('account_id', null);
        if ($this->account_id) { // it's an edit action
          $account = Doctrine_Core::getTable('Account')->find($this->account_id);
          $this->jobboard_id = $account->jobboard_id;
          $this->form = new AccountForm($account);
        } else {
          $this->jobboard_id = $request->getParameter('jobboard_id');
          $this->form = new AccountForm();
        }
      }
    return sfView::SUCCESS;
  }


  public function executeDelete(sfWebRequest $request)
  {
    $jobboard_id = $request->getParameter('jobboard_id', null);
    $jobboard = Doctrine_Core::getTable('Jobboard')->find($jobboard_id);
    if ($jobboard) {
      $jobboard->is_active = false;
      $jobboard->save();

      // now deactive all marques, products of this jobboard
      try {
        $marques = $jobboard->Marques;
        foreach ($marques as $marque) {
          $catalogs = $marque->Catalogs;
          foreach ($catalogs as $catalog) {
              $brands = $catalog->Brands;
              foreach ($brands as $brand) {
                $products = $brand->Products;
                foreach ($products as $product) {
                  $product->is_active = false;
                  $product->save();
                }
              }
          }
        }
        $this->getRequest()->setParameter('notify', "jobboard_deactive_success");
      } catch (Exception $e) {
          $this->getRequest()->setParameter('notify', "jobboard_deactive_failed");
      }
      
    }
    
    $this->forward('jobboard', 'list');
  }


  public function executeEdit(sfWebRequest $request)
  {
    $data = $request->getParameter('jobboard', null);
    if ($data) {
        $jobboard = Doctrine_Core::getTable('Jobboard')->findOneByName($data['name']);
        $is_reactivate = false;
        if (!$data['jobboard_id'] && $jobboard)
            if ($jobboard->is_active == false) {
              $is_reactivate = true;
              $jobboard->is_active = true;
              $jobboard->save();
            } else {
              $this->notify = "website_exists";
              $this->jobboard_form = new JobboardForm();
              $this->jobboard_form->bind($data);
              return sfView::SUCCESS;
            }
        if ($data['jobboard_id'])
          $jobboard = Doctrine_Core::getTable('Jobboard')->find($data['jobboard_id']);
        elseif (!$is_reactivate)
          $jobboard = new Jobboard();
          
        try {
          $jobboard->name = $data['name'];
          $jobboard->alias = $data['alias'];
          $jobboard->generator_name = $data['generator_name'];
          $jobboard->address = $data['address'];
          $jobboard->currency_id = $data['currency_id'];
          if (!$is_reactivate)
            $jobboard->is_active = isset($data['is_active']) ? true : false;
          $jobboard->save();
          $this->notify = "create_website_success";
          $this->jobboard_form = new JobboardForm($jobboard);
        } catch (Exception $e) {
          $this->notify = "create_website_failed";
        }
      } else {
        $this->jobboard_id = $request->getParameter('jobboard_id', null);
        if ($this->jobboard_id) { // it's an edit action
          $jobboard = Doctrine_Core::getTable('Jobboard')->find($this->jobboard_id);
          $this->jobboard_form = new JobboardForm($jobboard);
        } else {
          $this->jobboard_form = new JobboardForm();
        }
      }
    return sfView::SUCCESS;
  }

  public function executeRestrictionList(sfWebRequest $request)
  {
    $this->jobboard_id = $request->getParameter('jobboard_id');
    $this->notify = $request->getParameter('notify', null);
    if ($this->jobboard_id) {
      $q = Doctrine_Query::create()
            ->from('MarqueRestriction mr')
            ->where('mr.jobboard_id = ?', $this->jobboard_id);
      $this->restriction_marques = $q->execute();
      return sfView::SUCCESS;
    }
  }

  public function executeAddRestrictionMarque(sfWebRequest $request)
  {
    $data = $request->getParameter('marque_restriction', null);
    if ($data) {
      $marque = Doctrine_Core::getTable('MarqueRestriction')->find(array($data['jobboard_id'], $data['name']));
      if ($marque) {
        $this->notify = "marque_restriction_exists";
      } else {
        $marque = new MarqueRestriction();
        $marque->jobboard_id = $data['jobboard_id'];
        $marque->name = $data['name'];
        $marque->save();
        $this->notify = "create_success";  
      }
    }

    $this->restriction_marque_form = new MarqueRestrictionForm();
    $jobboard_id = $request->getParameter('jobboard_id', null);
    if (!$jobboard_id)
      $jobboard_id = $data['jobboard_id'];
    $jobboard = Doctrine_Core::getTable('Jobboard')->find($jobboard_id);
    $this->restriction_marque_form->setDefault('jobboard_id', array($jobboard_id, $jobboard->name));

    return sfView::SUCCESS;
  }


  public function executeDeleteRestrictionMarque(sfWebRequest $request)
  {
    $jobboard_id = $request->getParameter('jobboard_id', null);
    $name = $request->getParameter('name');
    $marque = Doctrine_Core::getTable('MarqueRestriction')->find(array($jobboard_id, $name));
    if ($marque) {
      $marque->delete();
      $this->notify = "marque_restriction_delete_success";
    } else {
      $this->notify = "marque_restriction_not_found";
    }
    $this->getRequest()->setParameter('notify', $this->notify);
    $this->getRequest()->setParameter('jobboard_id', $jobboard_id ? $jobboard_id : 1);
    $this->forward('jobboard', 'restrictionList');
  }
}